package com.mac.hosp_pre.user.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mac.hosp_pre.admin.feign.DictClientFeign;
import com.mac.hosp_pre.enums.DictEnum;
import com.mac.hosp_pre.except.ResultCodeEnum;
import com.mac.hosp_pre.except.ServiceException;
import com.mac.hosp_pre.reply.R;
import com.mac.hosp_pre.user.entity.Patient;
import com.mac.hosp_pre.user.mapper.PatientMapper;
import com.mac.hosp_pre.user.model.resp.PatientRespDto;
import com.mac.hosp_pre.user.service.PatientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private DictClientFeign dictFeignClient;

    //获取就诊人列表
    @Override
    public List<PatientRespDto> findAllUserId(Long userId) {
        //根据userid查询所有就诊人信息列表
        LambdaQueryWrapper<Patient> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Patient::getUserId,userId);
        List<Patient> patientList = baseMapper.selectList(wrapper);
        if (CollectionUtil.isEmpty(patientList)){
            return null;
        }
        //通过远程调用，得到编码对应具体内容，查询数据字典表内容
        List<PatientRespDto> collect = patientList.stream().map(item -> {
            return this.fillPatient(item);
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public PatientRespDto getPatientId(Long id) {
        return this.fillPatient(baseMapper.selectById(id));
    }

    //Patient对象里面其他参数封装
    private PatientRespDto fillPatient(Patient patient) {
        if (patient == null){
            throw new ServiceException(ResultCodeEnum.NOT_EXISTS_ERROR);
        }
        PatientRespDto respDto = new PatientRespDto();
        BeanUtils.copyProperties(patient, respDto);
        //根据证件类型编码，获取证件类型具体指
        R<String> res = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificateType() + "");//联系人证件
        String certificatesTypeString = "";
        if (res.ifSuccess()){
            certificatesTypeString = res.getData();
        }
        //联系人证件类型
        res = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),patient.getContactCertificatesType()+"");
        String contactsCertificatesTypeString = "";
        if (res.ifSuccess()){
            contactsCertificatesTypeString = res.getData();
        }
        //省
        res = dictFeignClient.getName(patient.getProvinceCode());
        String provinceString = "";
        if (res.ifSuccess()){
            provinceString = res.getData();
        }
        //市
        res = dictFeignClient.getName(patient.getCityCode());
        String cityString = "";
        if (res.ifSuccess()){
            cityString = res.getData();
        }
        //区
        res = dictFeignClient.getName(patient.getDistrictCode());
        String districtString = "";
        if (res.ifSuccess()){
            districtString = res.getData();
        }

        respDto.setCertificateTypeString(certificatesTypeString);
        respDto.setContactCertificatesTypeString(contactsCertificatesTypeString);
        respDto.setProvinceString(provinceString);
        respDto.setCityString(cityString);
        respDto.setDistrictString(districtString);
        respDto.setFullAddress(provinceString +"/"+ cityString +"/"+ districtString +"/"+ patient.getAddress());
        return respDto;
    }
}
